package com.avic.modules.equipment.rest;


import com.avic.dozer.service.IGenerator;
import com.avic.modules.equipment.service.EquipmentService;
import com.avic.modules.equipment.service.dto.EquipmentDto;
import com.avic.modules.equipment.service.dto.EquipmentQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**
 * @author zhangzikang
 * @date 2022-08-14
 */
@Api(tags = "健康管理: 设备健康状态接口")
@RestController
@RequestMapping("/api/equipment/healthy")
@AllArgsConstructor
public class EquipmentHealthyController {

    private final EquipmentService equipmentService;
    private final IGenerator generator;

    /**
     * 查看设备健康状态
     * @param criteria
     * @return
     */
    @ApiOperation("查看设备健康状态")
    @GetMapping
    @PreAuthorize("@el.check('admin','healthy:list')")
    public ResponseEntity<Object> getEquipment(EquipmentQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(equipmentService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    /**
     * 查看设备运行状态
     * @param criteria
     * @return
     */
    @ApiOperation("查看设备运行状态")
    @GetMapping("/function")
    @PreAuthorize("@el.check('admin','function:list')")
    public ResponseEntity<Object> getFunction(EquipmentQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(equipmentService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    /**
     * 导出设备健康状态信息
     * @param response
     * @param criteria
     */
    @ApiOperation("导出设备健康状态信息")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','healthy:download')")
    public void download(HttpServletResponse response, EquipmentQueryCriteria criteria, Pageable pageable) throws IOException {

        equipmentService.export(generator.convert(equipmentService.queryAlls(criteria,pageable), EquipmentDto.class), response);
    }

    /**
     * 导出设备运行状态信息
     * @param response
     * @param criteria
     */
    @ApiOperation("导出设备运行状态信息")
    @GetMapping(value = "/function/download")
    @PreAuthorize("@el.check('admin','function:download')")
    public void export(HttpServletResponse response, EquipmentQueryCriteria criteria, Pageable pageable) throws IOException {

        equipmentService.FunctionExport(generator.convert(equipmentService.queryAlls(criteria,pageable), EquipmentDto.class), response);
    }

    /**
     * 删除设备健康状态信息
     * @param ids
     * @return
     */
    @ApiOperation("删除设备健康状态信息")
    @DeleteMapping
    @PreAuthorize("@el.check('admin','healthy:del')")
    public ResponseEntity<Object> delEquipment(@RequestBody Set<String> ids){
        equipmentService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
